import React, { Component, createRef } from 'react'
import { Form, Button, Card, message, Spin } from 'antd';
import TextArea from 'antd/lib/input/TextArea';
import { Store } from 'antd/lib/form/interface';
import { platformOverviewReq, modifyPlatformOverviewReq } from '../../../../requests/indexPage';
import { FormInstance } from 'antd/lib/form';
import { getRes } from "../../../../configFile";

interface IProps { }
interface IState {
  isLoading: boolean,
}


export default class PlatformOverviewChange extends Component<IProps, IState> {
  //表单ref
  formRef = createRef<FormInstance>()
  constructor(props: IProps) {
    super(props)
    this.state = {
      isLoading: true,
    }
  }

  async componentDidMount() {
    try {
      const res = getRes(await platformOverviewReq())
      console.log('之前的信息' + res.data)
      if (res.isSuccessful === true) {
        if (this.formRef.current) {
          this.formRef.current.setFieldsValue({ 'platformOverview': res.data })
        }
      } else {
        message.error('平台简介获取失败')
      }
    } catch (err) {
      message.error(err.toString())
    } finally {
      this.setState({
        ...this.state,
        isLoading: false
      })
    }
  }

  //表单提交
  onSubmit: ((value: Store) => void) | undefined = async value => {
    this.setState({
      ...this.state,
      isLoading: true
    })
    const res = getRes(await modifyPlatformOverviewReq(value.platformOverview))
    if (res.isSuccessful === true) {
      message.success('修改成功')
    } else {
      message.error(res.message)
    }
    this.setState({
      ...this.state,
      isLoading: false
    })
  }

  render() {
    return (
      <Card title="更改平台概况" bordered={false} style={{ width: '100%' }}>
        <Spin spinning={this.state.isLoading}>
          <Form ref={this.formRef} layout='vertical' onFinish={this.onSubmit}>
            <Form.Item name="platformOverview" rules={[{ required: true, message: '不能为空' }]}>
              <TextArea rows={10} />
            </Form.Item>

            <Form.Item>
              <Button type="primary" htmlType="submit">
                提交
          </Button>
            </Form.Item>
          </Form>
        </Spin>
      </Card>
    )
  }
}
